import { defineStore } from 'pinia';
import { classList, tagList } from '#/api';

interface ClassItem {
  classId: string;
  className: string;
}

export const useStoreStore = defineStore('storeStore', {
  state: () => ({
    storeId: '',
    classList: [] as ClassItem[],
    tagList: [],
  }),
  getters: {
    getStoreId: (state) => () => {
      return state.storeId;
    },
    classData: (state) => () => {
      return state.classList;
    },
    tagData: (state) => () => {
      return state.tagList;
    },
  },
  actions: {
    setStoreId(storeId: string) {
      this.storeId = storeId;
      console.log('this.storeId', this.storeId);
    },
    setClassList(data: []) {
      this.classList = data;
      console.log('this.classList', this.classList);
    },
    setTagList(data: []) {
      this.tagList = data;
      console.log('this.tagList', this.tagList);
    },
    queryClassList(params: any): Promise<boolean> {
      return new Promise((resolve, reject) => {
        classList(params)
          .then((res) => {
            this.setClassList(res.data);
            resolve(true);
          })
          .catch((err) => {
            resolve(false);
          });
      });
    },
    queryTagList(params: any): Promise<boolean> {
      return new Promise((resolve, reject) => {
        tagList(params)
          .then((res) => {
            this.setTagList(res.data);
            resolve(res.data);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
  },
});
